package com.goldcn.sys.controller;

import com.goldcn.interceptor.CurrentUser;
import com.goldcn.common.BaseCode;
import com.goldcn.common.ResultData;
import com.goldcn.sys.model.util.Menu;
import com.goldcn.sys.service.SysPageService;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping(value = "/main")
@Api(value = "/main", description = "主页面")
public class SysMainController {

    @Autowired
    SysPageService sysPageService;

    @ApiOperation(value = "查询角色菜单")
    @RequestMapping(value = "/allMenus", method = RequestMethod.POST)
    public Object getAllMenus(HttpServletRequest request) {
        try {
            CurrentUser user = (CurrentUser) request.getAttribute("user");
            List<String> roles = Lists.newArrayList(user.getRolesID().split(","));
            if (roles == null || roles.size() == 0) {
                ResultData resultData = new ResultData(BaseCode.ERROR_CODE100);
                return resultData;
            }
            List<Menu> menus = sysPageService.allMenus(roles);
            return   menus;
        } catch (Exception e) {
            return new ResultData(BaseCode.SUCCESS_CODE);
        }
    }



}
